#!/bin/bash

start=6
stop=20

if [ "$1" == "" ]; then
    echo Usage: $0 "["conv\|cconv"]" "["start"]" "["stop"]"
    exit
fi

divisor=0;

if [ "$1" == "conv" ]; then
    shift=1;
    offset=2;
    divisor=3;
    outdir=timings1r
fi
if [ "$1" == "cconv" ]; then
    outdir=timings1c
fi

if [ "$outdir" == "" ]; then
    echo "not implemented"
    exit
fi

mkdir -p $outdir
cd $outdir
rm -f error.explicit error.implicit
cd ..

if [ "$2" != "" ]; then
    start=$2
fi

if [ "$3" != "" ]; then
    stop=$3
fi

echo Calculating error:
for (( i=$start; i<=$stop; i++ ))
do
    echo $i
    m=$(asy -c "2^$i")
    echo -e "$m \t $(nice -n 19 ./$1 -i -N1 -t -m $m | grep error | sed s/error=//)"| cat >> $outdir/error.implicit
    if [ "$divisor" != "0" ]; then
	m=$(asy -c "quotient(2^($i+$shift)+$offset,$divisor)")
    fi
    echo -e "$m \t $(nice -n 19 ./$1 -e -N1 -t -m $m | grep error | sed s/error=//)" | cat >> $outdir/error.explicit
done
